package com.kingboy.kbg.devop.scmmanager;

import sonia.scm.NotSupportedFeatuerException;
import sonia.scm.client.*;
import sonia.scm.group.Group;
import sonia.scm.repository.Repository;
import sonia.scm.user.User;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Create By 郭金宝 on 2018/6/29.<br>
 */
public class scmTest {

    static final String url = "http://114.115.216.133:8080/scm";
    static final String username = "scmadmin";
    static final String password = "kingboy";

    public static void main(String[] args) throws NotSupportedFeatuerException, IOException {
        ScmClientSession session = ScmClient.createSession(url, username, password);

        System.out.println(session.getUserHandler().getAll().get(1).getName());


        //Create a new repository:

//        Repository repository = new Repository();
//        repository.setName("scm-kbg");
//        repository.setType("git");
//        repository.setDescription("The easiest way to share and manage your Git, Mercurial and Subversion repositories over http.");

        // set permissions for user kingboy to owner
//        List<Permission> permissions = new ArrayList<Permission>();
//        permissions.add(new Permission("scmadmin", PermissionType.OWNER));
//        repository.setPermissions(permissions);
//        repository.setDescription("通过API接口创建的项目！");
//        repository.setContact("kingboy0502@qq.com");
//        //创建
//        try {
//            session.getRepositoryHandler().create(repository);
//            System.out.println("创建"+repository.getName()+"成功！");
//        } catch (Exception e) {
//            System.out.println("创建失败！");
//        }
        //Get the last 20 commits of a repository:
//        RepositoryClientHandler repositoryHandler = session.getRepositoryHandler();
//        // get the mercurial (hg) repository scm-manager
//        Repository repository = repositoryHandler.get("git", "KBG");
//        System.out.println("repository name is : "+repository.getName());
//        ClientChangesetHandler changesetHandler = repositoryHandler.getChangesetHandler(repository);
//        // get 20 changesets started by 0
//        ChangesetPagingResult changesets = changesetHandler.getChangesets(0, 1);
//        System.out.println("总共提交的次数："+changesets.getTotal());
//        for (Changeset c : changesets) {
//            System.out.println(c.getId() + ": " + c.getDescription()+"  " + c.getAuthor());
//        }

//        Print the content of a file in a repository:

//        RepositoryClientHandler repositoryHandler = session.getRepositoryHandler();
//// get the mercurial (git) repository scm-manager
//        Repository repository = repositoryHandler.get("git", "KBG");
//        System.out.println("repository Description is : "+repository.getDescription());
//        ClientRepositoryBrowser browser = repositoryHandler.getRepositoryBrowser(repository);
//        BufferedReader reader = null;
//        try {
//            // get the content of the file pom.xml at revision tip
//            reader = new BufferedReader(new InputStreamReader(browser.getContent("tip", "pom.xml")));
//            String line = null;
//            try {
//                line = reader.readLine();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            while ( line != null ){
//                System.out.println( line );
//                line = reader.readLine();
//            }
//        } finally {
//            if ( reader != null ){
//                reader.close();
//            }
//        }
//        Create a new user:

//        User user = new User("tuser", "Test User", "test.user@test.net");
//        user.setPassword("test123");
//        session.getUserHandler().create(user);

//        Add a user to an existing group:

        GroupClientHandler groupHandler = session.getGroupHandler();
        Group group = groupHandler.get("developers");
        System.out.println("group.getMembers is ： "+group.getMembers().size());
        group.getMembers().add("tuser");
        //删除的用户
        group.getMembers().remove("kingboy");
        //添加用户给组
        groupHandler.modify(group);
        session.close();
    }
}
